package controllers.games;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import models.games.PallonciniMatematici;
import models.util.BalloonThread;
import views.games.PallonciniMatematiciView;
import core.GameLauncher;
import core.GameLauncher.State;

public class PallonciniMatematiciController implements ActionListener {

	private PallonciniMatematiciView view;
	private PallonciniMatematici model;
	private GameLauncher gameLauncher;
	private Random random = new Random();
	private BalloonGenerator bg;
	private int generati = 0;

	public PallonciniMatematiciController(PallonciniMatematici model,
			PallonciniMatematiciView view, GameLauncher gameLauncher) {
		this.view = view;
		this.model = model;
		this.gameLauncher = gameLauncher;
		view.setOther("ERRORI RIMANENTI " + model.getRemainingErrors());
		view.setOtherThanClock("GENERATI " + 0);
		view.addActionListenerForInput(new InputActionListener());
		BalloonGenerator bg = new BalloonGenerator();
		bg.execute();
	}

	private class InputActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent a) {
			if (a.getSource() instanceof JButton) {
				JButton b = (JButton) a.getSource();
				String inputText = b.getText();
				if (inputText.equals("Ricomincia")) {
					PallonciniMatematici newModel = new PallonciniMatematici(
							model.getCurrentUser());
					PallonciniMatematiciView newView = new PallonciniMatematiciView();
					PallonciniMatematiciController newController = new PallonciniMatematiciController(
							newModel, newView, gameLauncher);
					gameLauncher.setGame(newModel, newView, newController);
					gameLauncher.startTransition(State.GAME);
					model.addGameToDatabase();
					System.out.println("Ricomincia");
				}

				else if (inputText.equals("Menu Giochi")) {
					gameLauncher.startTransition(State.GAME_MENU);
					model.addGameToDatabase();
					System.out.println("Menu Giochi");
				}
				return;
			} else if (model.gameIsFinished()) {
				view.setOther("ERRORI RIMANENTI " + model.getRemainingErrors());
				view.gameEnded();
				return;
			}

			JTextField t = (JTextField) a.getSource();
			String input = t.getText();
			int res;
			try {
				res = Integer.parseInt(input);
			} catch (NumberFormatException e) {
				t.setText("");
				return;
			}
			t.setText("");
			if (model.verify(res))
				view.setScore(model.getScore());
		}
	}

	public void updateRemainingErrors() {
		view.setOther("ERRORI RIMANENTI " + model.getRemainingErrors());
		if (model.getRemainingErrors() == 0) {
			view.gameEnded();
		}
	}

	private class BalloonGenerator extends SwingWorker<Void, Void> {

		private int delayMax = 5;
		private int delayMin = 2;

		@Override
		protected Void doInBackground() {
			while (!model.gameIsFinished()) {
				// System.out.println("STO PER GENERARE UN PALLONCINO");
				BalloonThread bt = model.generateBalloon();
				bt.setController(PallonciniMatematiciController.this);
				bt.setView(view);
				// System.out.println("STO PER AGGIUNGERE IL PALLONCINO "+bt);
				view.addBalloon(bt);
				generati++;
				view.setOtherThanClock("GENERATI " + generati);
				try {
					TimeUnit.SECONDS.sleep(random.nextInt(delayMax - delayMin
							+ 1)
							+ delayMin);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			return null;
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

}
